package leetcode.D100.T41;

/**
 * @Author: 18362
 * @Create: 2022-09-20 0:32:03 星期二
 */
class Solution {
    public int firstMissingPositive(int[] nums) {
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] == i + 1)
                continue;
            int k = nums[i];
            while (k > 0 && k <= nums.length && nums[k-1] != k) {
                int t = nums[k-1];
                nums[k-1] = k;
                k = t;
            }
        }
        for (int i=0; i<nums.length; ++i) {
            if (nums[i] != i+1)
                return i+1;
        }
        return nums.length + 1;
    }
}
